<template>
  <div class="app-container">
    <!-- <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="证件号码" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入证件号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="单位名称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入单位名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="社保编号" prop="ssnumber">
        <el-input
          v-model="queryParams.ssnumber"
          placeholder="请输入社保编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失业保险参保时间" prop="unemploymentNsuranceStartTime">
        <el-date-picker clearable
          v-model="queryParams.unemploymentNsuranceStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择失业保险参保时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="失业保险应缴月数" prop="unemploymentNsuranceYMonths">
        <el-input
          v-model="queryParams.unemploymentNsuranceYMonths"
          placeholder="请输入失业保险应缴月数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失业保险实缴月数" prop="unemploymentNsuranceSMonths">
        <el-input
          v-model="queryParams.unemploymentNsuranceSMonths"
          placeholder="请输入失业保险实缴月数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="失业保险欠费月数" prop="unemploymentNsuranceQMonths">
        <el-input
          v-model="queryParams.unemploymentNsuranceQMonths"
          placeholder="请输入失业保险欠费月数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工伤保险参保时间" prop="industrialInjuryInsuranceStartTime">
        <el-date-picker clearable
          v-model="queryParams.industrialInjuryInsuranceStartTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择工伤保险参保时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="工伤保险应缴月数 " prop="industrialInjuryInsuranceYMonths">
        <el-input
          v-model="queryParams.industrialInjuryInsuranceYMonths"
          placeholder="请输入工伤保险应缴月数 "
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工伤保险实缴月数" prop="industrialInjuryInsuranceSMonths">
        <el-input
          v-model="queryParams.industrialInjuryInsuranceSMonths"
          placeholder="请输入工伤保险实缴月数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工伤保险欠费月数" prop="industrialInjuryInsuranceQMonths">
        <el-input
          v-model="queryParams.industrialInjuryInsuranceQMonths"
          placeholder="请输入工伤保险欠费月数"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['business:management:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:management:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:management:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['business:management:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementList" @selection-change="handleSelectionChange" size="small">
      <el-table-column type="selection" width="55" align="center" show-overflow-tooltip/>
      <el-table-column label="ID" align="center" prop="id" width="50" show-overflow-tooltip/>
      <el-table-column label="姓名" align="center" prop="name" show-overflow-tooltip/>
      <el-table-column label="证件号码" align="center" prop="idcard" show-overflow-tooltip/>
      <el-table-column label="单位名称" align="center" prop="companyName" show-overflow-tooltip/>
      <el-table-column label="社保编号" align="center" prop="ssnumber" :show-overflow-tooltip="true" width="188">
        <template #default="scope">
               <!-- <router-link :to="'/wang/?id=' + scope.row.id" class="link-type">
                  <span>{{ scope.row.ssnumber }}</span>
               </router-link> -->
               <!-- <span @click = 'toDetail(scope.row.id)'>{{ scope.row.ssnumber }}</span> -->
               <a @click = 'toDetail(scope.row.id)' style="color: #409EFF;"> {{ scope.row.ssnumber }} </a>
            </template>
      </el-table-column>
       <el-table-column label="性别" align="center" prop="sex" show-overflow-tooltip>
              <template #default="scope">
                <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
              </template>
      </el-table-column>

      <el-table-column label="失业保险参保时间" align="center" prop="unemploymentNsuranceStartTime" width="180" show-overflow-tooltip/>
      <el-table-column label="失业保险应缴月数" align="center" prop="unemploymentNsuranceYMonths" show-overflow-tooltip/>
      <el-table-column label="失业保险实缴月数" align="center" prop="unemploymentNsuranceSMonths" show-overflow-tooltip/>
      <el-table-column label="失业保险欠费月数" align="center" prop="unemploymentNsuranceQMonths" show-overflow-tooltip/>
      <el-table-column label="工伤保险参保时间" align="center" prop="industrialInjuryInsuranceStartTime" width="180" show-overflow-tooltip/>
      <el-table-column label="工伤保险应缴月数 " align="center" prop="industrialInjuryInsuranceYMonths" show-overflow-tooltip/>
      <el-table-column label="工伤保险实缴月数" align="center" prop="industrialInjuryInsuranceSMonths" show-overflow-tooltip/>
      <el-table-column label="工伤保险欠费月数" align="center" prop="industrialInjuryInsuranceQMonths" show-overflow-tooltip/>
      <!-- <el-table-column label="二维码" align="center" prop="industrialInjuryInsuranceQMonths" >
        <el-image style="width: 100px; height: 100px" :src="image" :zoom-rate="1.2"
                                        :max-scale="7" :min-scale="0.2" :preview-src-list="srcList" :initial-index="4"
                                        fit="cover" />
      </el-table-column> -->
      
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['business:management:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['business:management:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改证书管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="managementRef" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="证件号码" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入证件号码" />
        </el-form-item>
        <el-form-item label="单位名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入单位名称" />
        </el-form-item>
        <el-form-item label="社保编号" prop="ssnumber">
          <el-input v-model="form.ssnumber" placeholder="请输入社保编号" />
        </el-form-item>
        <el-form-item label="用户性别">
                     <el-select v-model="form.sex" placeholder="请选择">
                        <el-option
                           v-for="dict in sys_user_sex"
                           :key="dict.value"
                           :label="dict.label"
                           :value="dict.value"
                        ></el-option>
                     </el-select>
                  </el-form-item>
        <el-form-item label="失业保险参保时间" prop="unemploymentNsuranceStartTime">
          <el-date-picker clearable
            v-model="form.unemploymentNsuranceStartTime"
            type="monthrange"
            range-separator="至"
            value-format="YYYYMM"
            start-placeholder="开始时间"
        end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="失业保险应缴月数" prop="unemploymentNsuranceYMonths">
          <el-input v-model="form.unemploymentNsuranceYMonths" placeholder="请输入失业保险应缴月数" type = "number" />
        </el-form-item>
        <el-form-item label="失业保险实缴月数" prop="unemploymentNsuranceSMonths">
          <el-input v-model="form.unemploymentNsuranceSMonths" placeholder="请输入失业保险实缴月数" type = "number" />
        </el-form-item>
        <el-form-item label="失业保险欠费月数" prop="unemploymentNsuranceQMonths">
          <el-input v-model="form.unemploymentNsuranceQMonths" placeholder="请输入失业保险欠费月数" type = "number" />
        </el-form-item>
        <el-form-item label="工伤保险参保时间" prop="industrialInjuryInsuranceStartTime">
          <el-date-picker clearable
            v-model="form.industrialInjuryInsuranceStartTime"
            type="monthrange"
            range-separator="至"
            value-format="YYYYMM"
            start-placeholder="开始时间"
        end-placeholder="结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="工伤保险应缴月数 " prop="industrialInjuryInsuranceYMonths">
          <el-input v-model="form.industrialInjuryInsuranceYMonths" placeholder="请输入工伤保险应缴月数 " type = "number" />
        </el-form-item>
        <el-form-item label="工伤保险实缴月数" prop="industrialInjuryInsuranceSMonths">
          <el-input v-model="form.industrialInjuryInsuranceSMonths" placeholder="请输入工伤保险实缴月数" type = "number" />
        </el-form-item>
        <el-form-item label="工伤保险欠费月数" prop="industrialInjuryInsuranceQMonths">
          <el-input v-model="form.industrialInjuryInsuranceQMonths" placeholder="请输入工伤保险欠费月数" type = "number" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Management">
import { listManagement, getManagement, delManagement, addManagement, updateManagement } from "@/api/business/management";

const { proxy } = getCurrentInstance();
const { sys_user_sex } = proxy.useDict( "sys_user_sex");

const managementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    idcard: null,
    companyName: null,
    ssnumber: null,
    sex: null,
    unemploymentNsuranceStartTime: null,
    unemploymentNsuranceYMonths: null,
    unemploymentNsuranceSMonths: null,
    unemploymentNsuranceQMonths: null,
    industrialInjuryInsuranceStartTime: null,
    industrialInjuryInsuranceYMonths: null,
    industrialInjuryInsuranceSMonths: null,
    industrialInjuryInsuranceQMonths: null
  },
  rules: {
    id: [
      { required: true, message: "ID不能为空", trigger: "blur" }
    ],
    name: [
      { required: true, message: "姓名不能为空", trigger: "blur" }
    ],
    idcard: [
      { required: true, message: "证件号码不能为空", trigger: "blur" }
    ],
    companyName: [
      { required: true, message: "单位名称不能为空", trigger: "blur" }
    ],
    ssnumber: [
      { required: true, message: "社保编号不能为空", trigger: "blur" }
    ],
    sex: [
      { required: true, message: "性别不能为空", trigger: "change" }
    ],
    unemploymentNsuranceStartTime: [
      { required: true, message: "失业保险参保时间不能为空", trigger: "blur" }
    ],
    unemploymentNsuranceYMonths: [
      { required: true, message: "失业保险应缴月数不能为空", trigger: "blur" }
    ],
    unemploymentNsuranceSMonths: [
      { required: true, message: "失业保险实缴月数不能为空", trigger: "blur" }
    ],
    unemploymentNsuranceQMonths: [
      { required: true, message: "失业保险欠费月数不能为空", trigger: "blur" }
    ],
    industrialInjuryInsuranceStartTime: [
      { required: true, message: "工伤保险参保时间不能为空", trigger: "blur" }
    ],
    industrialInjuryInsuranceYMonths: [
      { required: true, message: "工伤保险应缴月数 不能为空", trigger: "blur" }
    ],
    industrialInjuryInsuranceSMonths: [
      { required: true, message: "工伤保险实缴月数不能为空", trigger: "blur" }
    ],
    industrialInjuryInsuranceQMonths: [
      { required: true, message: "工伤保险欠费月数不能为空", trigger: "blur" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

function toDetail(id){
  console.log(id);
  window.open('../../../detail.html?id='+ id)
}

/** 查询证书管理列表 */
function getList() {
  loading.value = true;
  listManagement(queryParams.value).then(response => {
    managementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    idcard: null,
    companyName: null,
    ssnumber: null,
    sex: null,
    unemploymentNsuranceStartTime: null,
    unemploymentNsuranceYMonths: null,
    unemploymentNsuranceSMonths: null,
    unemploymentNsuranceQMonths: null,
    industrialInjuryInsuranceStartTime: null,
    industrialInjuryInsuranceYMonths: null,
    industrialInjuryInsuranceSMonths: null,
    industrialInjuryInsuranceQMonths: null
  };
  proxy.resetForm("managementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加证书管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getManagement(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改证书管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["managementRef"].validate(valid => {
    const unt = form.value.unemploymentNsuranceStartTime.join('-')
   form.value.unemploymentNsuranceStartTime = unt
   const iint = form.value.industrialInjuryInsuranceStartTime.join('-')
   form.value.industrialInjuryInsuranceStartTime = iint
   
    if (valid) {
      if (form.value.id != null) {
        updateManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除证书管理编号为"' + _ids + '"的数据项？').then(function() {
    return delManagement(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('business/management/export', {
    ...queryParams.value
  }, `management_${new Date().getTime()}.xlsx`)
}

getList();
</script>
